<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *客户列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$tabs=array(
	array('op'=>'list','title'=>'口碑成交'),
);
$return['stores']=$this->getall('store',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
$return['workers']=$this->getall('worker',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
//客户列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('koubei_storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'from_type'=>array(3,4),'status'=>array(10,11,12,13,14,15,16,17,18,19,20));
	if($_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type'];
	if($_RQ['storeid']){
		$params['koubei_storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	$list=pdo_getslice('jy_yxw_member',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('member')->member_show($item['id']);
			if($item['from_type']==3){
				$item['from_title']=pdo_getcolumn('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['from_xueshengid']),'title');
			}
			if($item['from_type']==4){
				$item['from_title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['from_workerid']),'title');
			}
			$item['xueshengid']=pdo_getcolumn('jy_yxw_xuesheng',array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['id']),'id');
			$item['isxuesheng']=$item['xueshengid']?1:0;
			$item['worker6title']=pdo_getcolumn('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['worker6id']),'title');
			$item['worker6title']=$item['worker6title']?:'无';
			if($item['workerheid']){
				$workerhe=pdo_get('jy_yxw_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['workerheid']),array('role','title'));
				$item['workerhetitle']=$this->user_role[$workerhe['role']].':'.$workerhe['title'];
			}
			$infee_log=pdo_get('jy_yxw_infee_log',array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['id']),array('id','koubei_storeid'));
			if(!$infee_log['koubei_storeid']){
				pdo_update('jy_yxw_infee_log',array('koubei_storeid'=>$item['koubei_storeid']),array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$item['id']));
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该客户吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>false,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			//array('name'=>'storeid','title'=>'所属门店','type'=>'select','options'=>'stores'),
			array('name'=>'jibie_type','title'=>'客户级别','type'=>'radio','options'=>'jibie_type'),
			array('name'=>'from_type','title'=>'渠道','type'=>'radio','options'=>'from_type'),
			array('type'=>'string','title'=>'口碑员工/售后','name'=>'from_title','help'=>''),
			array('name'=>'from1_type','title'=>'实体来源','type'=>'radio','options'=>'from1_type'),
			array('name'=>'from12_type','title'=>'网络来源','type'=>'radio','options'=>'from12_type'),
			array('name'=>'worker3id','title'=>'网络专员','type'=>'select','options'=>'workers'),
			array('name'=>'worker4id','title'=>'前台员','type'=>'select','options'=>'workers'),
			array('name'=>'worker5id','title'=>'电客员','type'=>'select','options'=>'workers'),
			array('type'=>'img_string','title'=>'合作成交',
            'data'=>array(
                array('title'=>'销售员:','name'=>'worker6title'),
                array('title'=>'','name'=>'workerhetitle'),
                )
            ),
			array('name'=>'worker7id','title'=>'体验员','type'=>'select','options'=>'workers'),
			array('name'=>'worker8id','title'=>'试用员','type'=>'select','options'=>'workers'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'添加日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','action'=>'jy_yxw/jiaoxue.koubeibao/setclass','title'=>'分配售后群','show'=>array('isxuesheng'=>0)),
			array('type'=>'modal','action'=>'jy_yxw/jiaoxue.koubeibao/workerheid','title'=>'设置合作人'),
			//array('type'=>'modal','op'=>'worker5id','title'=>'分配电客员','show'=>array('status'=>0)),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'手机号','name'=>'mobile','help'=>''),
			array('type'=>'string','width'=>'15','title'=>'客户名称','name'=>'title','help'=>''),
			//array('name'=>'status','width'=>'15','title'=>'状态','type'=>'radio','options'=>'status'),
		),
	);
	$return['status']=$this->member_status;
	$return['jibie_type']=$this->member_jibie_type;
	$return['from_type']=$this->member_from_type;
	$return['from1_type']=$this->member_from1_type;
	$return['from2_type']=$this->member_from2_type;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='setclass'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		if(!$post['classid']){
			exi('请选择售后群！','error');
		}
		$post['memberid']=$post['id'];
		$this->model('xuesheng')->addxuesheng($post,$_SESSION['storeid']);
		$class=$this->get('class',array('id'=>$post['classid'],'uniacid'=>$_SESSION['uniacid']));
		$uplog=array(
			'memberid'=>$id,
			'remark'=>'分配售后群',
			'status'=>1,
			'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',array('status'=>11,'worker11id'=>$class['workerid']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('操作成功！','','list');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
		$item['classid']=$item['classid']?:$item['toclassid'];
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'分配售后群',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'分配售后群','name'=>'classid','options'=>'class','help'=>'选择售后基地的[未开班的售后群+开班中的售后群]'),
	);
	$return['class']=$this->getall('class',array('status'=>array(0,1),'storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='workerheid'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=$post['id'];
		$update=array('workerheid'=>intval($post['workerheid']));
		$uplog=array(
			'memberid'=>$id,
			'remark'=>'设置合作人',
			'status'=>1,
			'type'=>2,
		);
		$this->model('uplog')->addgenjin_log($uplog);
		pdo_update('jy_yxw_member',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('jy_yxw_infee_log',$update,array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$id));
		pdo_update('jy_yxw_tuifee_log',$update,array('uniacid'=>$_SESSION['uniacid'],'memberid'=>$id));
		exi('操作成功！','','//list/status=all');
	}
	if($_RQ['id']){
		$item=pdo_get('jy_yxw_member',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		exi('请先选择客户','error');
	}
	$return['item']=$item;	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'设置合作人',
	);
	$htmls['edit']=array(
		array('type'=>'select','title'=>'设置合作人','name'=>'workerheid','options'=>'workers'),
	);
	$jiaoxueid=$this->getcolumn('store',array('id'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),'jiaoxueid');
	$return['workers']=$this->getall('worker',array('status'=>1,'role'=>array(6,9,10,11),'id <>'=>$_SESSION['workerid'],'uniacid'=>$_SESSION['uniacid']),array('id','title','storeid'),'id','storeid asc');	
	foreach($return['workers'] as &$wv){
		$storetitle=$this->getcolumn('store',array('id'=>$wv['storeid'],'uniacid'=>$_SESSION['uniacid']),'title');
		$wv['title']='['.$storetitle.']'.$wv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	//客户列表
	$return['params']=array();
	$params=array('koubei_storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid'],'from_type'=>array(3,4),'status'=>array(11,12,13,14,15,16,17,18,19,20));
	if($_RQ['jibie_type']!='all'){
		$params['jibie_type']=intval($_RQ['jibie_type']);
	}
	$return['params']['jibie_type']=$_RQ['jibie_type'];
	if($_RQ['status']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	if($_RQ['storeid']){
		$params['koubei_storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
	}
	if(!empty($_RQ['mobile'])){
		$params['mobile']=$_RQ['mobile'];
		$return['params']['mobile']=$_RQ['mobile'];
	}
	$list=pdo_getall('jy_yxw_member',$params,$fields);
	if($list){
		foreach($list as &$item){
			$item['createtime']=date('Y/m/d H:i:s',$item['createtime']);
			$item['jibie_type']=$this->member_jibie_type[$item['jibie_type']];
			$item['from_type']=$this->member_from_type[$item['from_type']];
			$item['status']=$this->member_status[$item['status']];
			$item['xueli']=$this->xueli_type[$item['xueli']];
		}
	}
	$header=array(
		'id'=>'客户ID',
		'title'=>'客户名称',
		'mobile'=>'手机号',
		'jibie_type'=>'级别',
		'from_type'=>'渠道',
		'xueli'=>'学历',
		'status'=>'状态',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'客户数据');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			$this->model('member')->check_del($id,'xuesheng');
			pdo_delete('jy_yxw_member',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}